package com.hotelManagerSystem.controller;


import com.github.pagehelper.PageInfo;
import com.hotelManagerSystem.model.Room;
import com.hotelManagerSystem.service.IRoomService;
import com.hotelManagerSystem.util.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;


import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Cynthia
 * @since 2021-12-22
 */
@Controller
@RequestMapping("/room")
public class RoomController {

    //注入业务接口
    @Autowired
    private IRoomService iRoomService;

    /*-----查询-----房间列表-----*/
    @RequestMapping("/list")
    public String list(Model model) {
        //内置的方法list
        List<Room> roomList_db=iRoomService.list();
        model.addAttribute("roomList_db",roomList_db);
        System.out.println("===查询房间列表===list()===");
        return "system/admin/room/roomList";
    }

    /*-----添加UI-----房间-----*/
    @RequestMapping("/addUI")
    public String addUI(Model model){
        System.out.println("===添加UI===addUI()===");
        return "system/admin/room/addRoom";
    }

    /*-----添加-----房间-----*/
    @RequestMapping("/add")
    public String add(Model model,Room room){
        //获取数据
        System.out.println(room);
        //调用业务
        boolean b=iRoomService.save(room);
        //返回roomList页面
        List<Room> roomList_db=iRoomService.list();
        model.addAttribute("roomList_db",roomList_db);
        System.out.println("===添加房间===add()===");
        return "system/admin/room/roomList";
    }

    /*-----删除-----通过roomId删除房间-----*/
    @RequestMapping("/delete")
    public String delete(Model model,int roomId){
        //获取数据
        System.out.println(roomId);
        //调用业务
        boolean b=iRoomService.removeById(roomId);
        //返回userList页面
        List<Room> roomList_db=iRoomService.list();
        model.addAttribute("roomList_db",roomList_db);
        System.out.println("===删除房间===delete()===");
        return "system/admin/room/roomList";
    }

    /*-----修改UI-----房间-----*/
    @RequestMapping("/updateUI")
    public String updateUI(Model model,int roomId){
        //接收数据
        System.out.println(roomId);
        //调用业务
        //通过 roomId房间ID查询对应的房间
        Room room_db=iRoomService.getById(roomId);
        model.addAttribute("room_db",room_db);
        System.out.println("===修改UI===updateUI()===");
        //返回updateRoom页面
        return "system/admin/room/updateRoom";
    }

    /*-----修改-----房间-----*/
    @RequestMapping("/update")
    public String update(Model model,Room room){
        //获取数据
        System.out.println(room);
        //调用业务进行更新
        boolean b=iRoomService.saveOrUpdate(room);
        //返回roomList页面
        List<Room> roomList_db=iRoomService.list();
        model.addAttribute("roomList_db",roomList_db);
        System.out.println("===修改房间===update()===");
        return "system/admin/room/roomList";
    }

    /**
     * @author treasy
     * @description 前往房间信息界面
     */
    @RequestMapping("/admin/list")
    public String roomList(Model model,
                           @RequestParam(required = false,defaultValue="1",value="pageNum")Integer pageNum,
                           @RequestParam(defaultValue="8",value="pageSize")Integer pageSize,
                           @RequestParam(required = false,value="roomOrderState")Integer roomOrderState){
        PageUtil.runStartPage(pageNum,pageSize);
        List<Room> roomList=null;
        if (roomOrderState==null){
            roomList = iRoomService.list();
        } else {
            roomList = iRoomService.queryInfoByRoomOrderState(roomOrderState);
        }
        PageInfo pageInfo = PageUtil.getPageInfo(roomList);
        model.addAttribute("pageInfo",pageInfo);
        //获取房间所有楼层信息
        return "/admin/roomPage/room-list";
    }
}

